package com.software.project.service;

import com.software.project.entity.Weapon;
import com.software.project.repository.WeaponRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class WeaponServiceImpl implements WeaponService{

    @Autowired
    private WeaponRepository weaponRepository;


    @Override
    public List<Weapon> findUnlockingWeapon(int intelligence, int power) {
        log.debug("intelligence: {}, power: {}", intelligence, power);
        List<Weapon> weaponList = weaponRepository.findByWeaponRequireIntelligenceLessThanEqualAndWeaponRequirePowerLessThanEqual(intelligence, power);
        log.debug("weaponList: {}", weaponList);
        return weaponList;
    }

    @Override
    public List<Weapon> findLockedWeapon(int intelligence, int power) {
        log.debug("intelligence: {}, power: {}", intelligence, power);
        List<Weapon> weaponList = weaponRepository.findByWeaponRequireIntelligenceGreaterThanOrWeaponRequirePowerGreaterThan(intelligence, power);
        log.debug("weaponList: {}", weaponList);
        return weaponList;
    }

    @Override
    public Weapon findWeaponByWeaponId(Integer weaponId) {
        log.debug("weaponId: {}", weaponId);
        Optional<Weapon> optional = weaponRepository.findById(weaponId);
        if (optional.isPresent()) {
            log.debug("weapon: {}", optional.get());
            return optional.get();
        } else {
            log.debug("weapon: {}", "null");
            return null;
        }
    }

    @Override
    public Weapon findWeaponByWeaponImage(String weaponImage) {
        log.debug("weaponImage: {}", weaponImage);
        Optional<Weapon> byWeaponSrc = weaponRepository.findByWeaponSrc(weaponImage);
        if (byWeaponSrc.isPresent()) {
            log.debug("weapon: {}", byWeaponSrc.get());
            return byWeaponSrc.get();
        } else {
            log.warn("weapon: {}", "null");
            return null;
        }
    }
}
